package com.sunnybear.framework.ui.refresh;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import com.caimuhao.rxpicker.utils.DensityUtil;
import com.sunnybear.framework.R;
import com.sunnybear.framework.databinding.LibLoadMoreFooterBinding;
import com.sunnybear.framework.tools.ResourcesUtils;
import com.sunnybear.framework.tools.log.Logger;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.CircularProgressDrawable;

/**
 *  2018/7/26.
 */

public class QuickLoadMoreFooter extends BaseLoadMoreFooter<LibLoadMoreFooterBinding> {

    private CircularProgressDrawable mProgress;

    private int size;
    private int height;


    public QuickLoadMoreFooter(@NonNull Context context) {
        this(context, null, 0);
    }

    public QuickLoadMoreFooter(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QuickLoadMoreFooter(@NonNull Context context, @Nullable AttributeSet attrs, int
            defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        size = DensityUtil.dp2px(getContext(), 35);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.lib_load_more_footer;
    }

    @Override
    public void dragToShow(float offset) {
        float value = offset / height;
        if (value > 1) {
            value = 1 + (value - 1) / 2;
        }
        Logger.d("zxl", "value:" + value);
        mProgress.setStartEndTrim(Math.max(0, value - 0.8f), value);
        mProgress.setArrowScale(Math.min(1f, value));
    }

    @Override
    public void releaseTouch() {
        mProgress.setArrowEnabled(false);
    }

    @Override
    public void startLoading() {
        mProgress.start();
    }

    @Override
    public void scaleFooterToHide(long time) {
        Animation scale = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if (interpolatedTime > 0.5f) {
                    interpolatedTime = 1;
                }
                mViewDataBinding.refreshProgress.setScaleX(1 - interpolatedTime);
                mViewDataBinding.refreshProgress.setScaleY(1 - interpolatedTime);
            }
        };
        scale.setDuration(time);
        mViewDataBinding.refreshProgress.startAnimation(scale);
    }

    @Override
    public void reset() {
        mProgress.setStartEndTrim(0, 0);
        mProgress.setArrowScale(0);
        mProgress.stop();
        mViewDataBinding.refreshProgress.clearAnimation();
        mViewDataBinding.refreshProgress.setScaleX(1);
        mViewDataBinding.refreshProgress.setScaleY(1);
        mViewDataBinding.refreshProgress.getLayoutParams().width = size;
        mViewDataBinding.refreshProgress.getLayoutParams().height = size;
        mProgress.setArrowEnabled(true);
    }

    /**
     * 初始化View
     */
    private void initView() {
        mProgress = new CircularProgressDrawable(getContext());
        mProgress.setStyle(CircularProgressDrawable.DEFAULT);
        mProgress.setColorSchemeColors(ResourcesUtils.getColor(getContext(), R.color.color_3296fa));
        mProgress.setArrowEnabled(true);
        mViewDataBinding.refreshProgress.setImageDrawable(mProgress);
        int[] values = unDisplayViewSize(this);
        height = values[1];
        Logger.d("zxl", "height:" + height);
    }

    public int[] unDisplayViewSize(View view) {
        int size[] = new int[2];
        int width = MeasureSpec.makeMeasureSpec(0,
                MeasureSpec.UNSPECIFIED);
        int height = MeasureSpec.makeMeasureSpec(0,
                MeasureSpec.UNSPECIFIED);
        view.measure(width, height);
        size[0] = view.getMeasuredWidth();
        size[1] = view.getMeasuredHeight();
        return size;
    }

}
